package com.example.common.utils;

import android.content.res.Resources;
import android.util.DisplayMetrics;

import com.example.common.MyApplication;

import java.lang.reflect.Field;

/**
 * 获取屏幕尺寸
 */
public class ScreenDimenUtils {

    private static ScreenDimenUtils sInstance;

    private int mStatusBarHeight;
    private int mContextHeight;
    private int mScreenWidth;
    private int mScreenHeight;

    private ScreenDimenUtils() {
        Resources mResources = MyApplication.sInstener.getResources();
        DisplayMetrics metrics = mResources.getDisplayMetrics();
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object object = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(object).toString());
            mStatusBarHeight = mResources.getDimensionPixelSize(x);
            mContextHeight = mScreenHeight - mStatusBarHeight;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ScreenDimenUtils getsInstance() {
        if (sInstance == null) {
            synchronized (ScreenDimenUtils.class) {
                if (sInstance == null) {
                    sInstance = new ScreenDimenUtils();
                }
            }
        }
        return sInstance;
    }

    public int getStatusBarHeight() {
        return mStatusBarHeight;
    }

    public int getContextHeight() {
        return mContextHeight;
    }

    public int getScreenWidth() {
        return mScreenWidth;
    }

    public int getScreenHeight() {
        return mScreenHeight;
    }
}
